/*
 * Create Author  : jinpeng.chen
 * Create Date    : 2017-01-11
 * Project        : springboot-chapter5
 * File Name      : ApplicationInit.java
 *
 * Copyright (c) 2010-2015 by Shanghai HanTao Information Co., Ltd.
 * All rights reserved.
 *
 */
package com.charles.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.charles.app.dao.CityRepository;
import com.charles.app.dao.ProvinceRepository;
import com.charles.app.domain.City;
import com.charles.app.domain.Province;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

/**
 * 功能描述:  <p>
 *
 * @author : jinpeng.chen <p>
 * @version 1.0 2017-01-11
 * @since springboot-chapter5 1.0
 */
@Component
public class ApplicationInit implements InitializingBean {

    @Autowired
    private ProvinceRepository provinceRepository;

    @Autowired
    private CityRepository cityRepository;


    @Override
    public void afterPropertiesSet() throws Exception {
        Resource province = new ClassPathResource("province.json");
        String p = province.getFile().toString();
        Resource city = new ClassPathResource("citys.json");
        String c = city.getFile().toString();
        JSONArray po = JSON.parseArray(p);

        java.util.List<Province> provinceList = Lists.newArrayList();
        java.util.List<City> cities = Lists.newArrayList();
        for (int i = 0; i < po.size(); i++) {
            Province province1 = new Province();
            JSONObject jsonObject = po.getJSONObject(i);
            province1.setProvinceName(jsonObject.getString("name"));
            province1.setProvinceId(jsonObject.getLong("code"));
            provinceList.add(province1);
        }
    }
}
